<template>
  <div class="contain">
    <h1 style="margin-top: 80px;color: white;">尬聊</h1>
    <div class="minputcontain">
      <x-input class="minput" v-model="username" type="text" placeholder="请输入账号"></x-input>

      <x-input class="minput" v-model="pasword" type="password" placeholder="请输入密码"></x-input>
      <x-button class="btn_login" type="primary" @click.native="login">登录</x-button>
      <div style="text-align: right;margin: 10px">
        <span style="color: white" @click="btn_register">注册</span>
      </div>


    </div>

    <alert v-model="showdialog" :content="dialog_content"></alert>
  </div>
</template>

<script>

  import XInput from "vux/src/components/x-input/index";
  import XButton from "vux/src/components/x-button/index";
  import Alert from "../../node_modules/vux/src/components/alert/index.vue";

  export default {
    name: "Login",
    data() {
      return {
        username: "",
        pasword: "",
        showdialog: false,
        dialog_content: ''
      }
    },
    methods: {
      login() {
        var vm = this;
        this.$http.post('/v1/users/login', {
          username: this.username,
          password: this.pasword
        }).then(function (response) {
          if (response.data.code === 1000) {
            vm.$store.commit('setUserInfo',response.data.data.user)
            vm.$router.push({
              path: '/home'
            })
          } else {
            vm.dialog_content = response.data.message
            vm.showdialog = true
          }
        }).catch(function (e) {
          vm.dialog_content = e.message
          vm.showdialog = true
        })

        // // 显示
        // // this.show = true
        // //replace 不存在历史栈中，无法回退
        // this.$router.push({
        //   path: '/home'
        // })
      },
      btn_register() {
        this.$router.push('/register')
      }
    },
    components: {
      Alert,
      XButton, XInput
    },
  }
</script>
<style scoped>
  .contain {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: url("../assets/login_bg3.jpg") no-repeat;
    background-size: 100% 100%;
    text-align: center;
  }

  .minput {
    border: none;
    background: #ebf5dd;
    margin-top: 20px;
    border-radius: 4px;
  }

  .btn_login {
    border-radius: 4px;
    margin-top: 30px;
  }

  .minputcontain {
    text-color: 'white';
    margin: 20px;
    margin-top: 100px;
    box-sizing: border-box;
  }

</style>
